<template>
  <page-container title="路线管理">
    <template #extra>
      <el-button @click="onAddRoute">添加路线</el-button>
    </template>
    <div class="mt-4">
      <el-input
        v-model="query"
        style="max-width: 600px"
        placeholder="请输入站点信息"
        class="input-with-select"
        clearable
        @clear="clear"
      >
        <template #append>
          <el-button :icon="Search" @click="findByQuery" />
        </template>
      </el-input>
    </div>
    <!-- v-loading="loading"这个是页面加载的loading效果 -->
    <el-table v-loading="loading" style="width: 100%" :data="RouteList">
      <el-table-column label="序号" width="50" type="index"></el-table-column>
      <el-table-column label="路线名称" prop="routeName"></el-table-column>
      <el-table-column label="起始站-终点站" prop="startToEndPoint"></el-table-column>
      <el-table-column label="总时间（分）" prop="sumTime"></el-table-column>
      <el-table-column label="路线详情">
        <template #default="{ row, $index }">
          <el-button type="primary" plain @click="showPointsDialog(row, $index)"
            >查看站点</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEdit(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="onDelRoute(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 添加分类弹框 -->
    <RouteEdit ref="dialog" @success="onSuccess"></RouteEdit>

    <el-dialog v-model="dialogVisible" title="路线详情" width="500">
      <el-timeline style="max-width: 600px">
        <el-timeline-item
          v-for="point in RoutePointList"
          :key="point.id"
          :timestamp="'[' + point.longitude + ',' + point.latitude + ']'"
        >
          {{ point.pointName }}
        </el-timeline-item>
      </el-timeline>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </page-container>
</template>

<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { DelRouteService, GetRouteService, GetRouteFindByPointName } from '@/api/route'
import RouteEdit from './components/RouteEdit.vue'
import { Search } from '@element-plus/icons-vue'
const dialog = ref()
const RouteList = ref([])

const query = ref('')
const findByQuery = async () => {
  loading.value = true
  console.log(query.value)
  const res = await GetRouteFindByPointName(query.value)
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  RouteList.value = res.data.data
  loading.value = false
}

const clear = () => {
  query.value = ''
  getRouteList()
}

// loading加载页面效果
const loading = ref(false)

const dialogVisible = ref(false)

const RoutePointList = ref([])

const showPointsDialog = (row) => {
  RoutePointList.value = row.pointList
  dialogVisible.value = true
}

const getRouteList = async () => {
  loading.value = true
  const res = await GetRouteService()
  console.log(res)
  if (res.data.meta.status !== 200) {
    ElMessage.error(res.data.meta.msg)
    return
  }
  RouteList.value = res.data.data
  loading.value = false
}

getRouteList()

//点击添加分类跳出弹框
const onAddRoute = () => {
  // dialogFormVisible.value = true
  dialog.value.open({})
}
//点击编辑的时候跳出弹框
const handleEdit = (row) => {
  dialog.value.open(row)
}

//点击删除,删除所在列表
const onDelRoute = async (row) => {
  await ElMessageBox.confirm('你确认要删除该路线么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await DelRouteService(row.id)
  ElMessage.success('删除成功')
  getRouteList()
}

const onSuccess = () => {
  getRouteList()
}
</script>
